package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.CustomerTransferMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerTransferService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTransferServiceImpl implements ICustomerTransferService {

    @Autowired
    private CustomerTransferMapper customerTransferMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void delete(Long id) {
        customerTransferMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(CustomerTransfer customerTransfer) {
        //客户id
        Long customerId = customerTransfer.getCustomer().getId();
        //新销售id
        Long newSellerId = customerTransfer.getNewseller().getId();
        customerMapper.updateBySellerId(customerId, newSellerId);
        //设置移交时间
        customerTransfer.setOperateTime(new Date());
        //设置当前登录人员为录入人
        customerTransfer.setOperator((Employee) SecurityUtils.getSubject().getPrincipal());
        //设置客户状态为前端传来的
        customerMapper.updateStatus(customerTransfer.getCustomer().getId(), customerTransfer.getCustomer().getStatus());
        customerTransferMapper.insert(customerTransfer);
    }

    @Override
    public CustomerTransfer get(Long id) {
        return customerTransferMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerTransfer> listAll() {
        return customerTransferMapper.selectAll();
    }

    @Override
    public void update(CustomerTransfer customerTransfer) {

        customerTransferMapper.updateByPrimaryKey(customerTransfer);
    }

    public PageInfo list(QueryObject queryObject) {
        int currentPage = queryObject.getCurrentPage();
        int pageSize = queryObject.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        List<CustomerTransfer> list = customerTransferMapper.list(queryObject);
        return new PageInfo(list);
    }
}
